 //99.恢复二叉搜索树
 //https://leetcode.cn/problems/recover-binary-search-tree/
class Solution {
public:
    void recoverTree(TreeNode* root) {
        if(!root) return;
        vector<int> nums;
        function<void(TreeNode*)> _recoverTree = [&](TreeNode* root){
            if(!root) return;
            _recoverTree(root->left);
            nums.push_back(root->val);
            _recoverTree(root->right);
        };
        _recoverTree(root);
        sort(nums.begin(),nums.end());
        int sz = nums.size();
        int n = 0;
        function<void(TreeNode*,int&)> wTree = [&](TreeNode* root,int& n){
            if(!root || sz == n) return;
            if(root->left) wTree(root->left,n);
            root->val = nums[n++];
            if(root->right) wTree(root->right,n);
        };
        wTree(root,n);
    }
};